/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormBarangJenis.java
 *
 * Created on Feb 4, 2011, 2:35:32 AM
 */

package juwita;

import config.DBConnection;
import java.sql.*;
//import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Abah
 */
public class FormBarangJenis extends javax.swing.JInternalFrame {
    private DefaultTableModel DftTabMode;
    private boolean isGolLoaded = false;
    //private String urlValue="";
    java.sql.Connection conn = new DBConnection().connect();

    /** Creates new form FormBarangJenis */
    public FormBarangJenis() {
        initComponents();
        loadDataGolongan();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        setKodeJenis();
        tampilData();
        setDataGolongan();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTengah = new javax.swing.JPanel();
        spBarangJenis = new javax.swing.JScrollPane();
        tbBarangJenis = new javax.swing.JTable();
        pnAtas = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        txtNamaGol = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtKdJenis = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtKet = new javax.swing.JTextArea();
        btnSearch = new javax.swing.JButton();
        txtNamaSearch = new javax.swing.JTextField();
        btnNew = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtNamaJenis = new javax.swing.JTextField();
        cbGol = new javax.swing.JComboBox();
        cbSearchJenis = new javax.swing.JComboBox();
        btnShowAll = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setForeground(new java.awt.Color(0, 0, 0));
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Master Jenis Barang");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pnTengah.setBackground(new java.awt.Color(255, 255, 255));
        pnTengah.setLayout(new java.awt.BorderLayout());

        tbBarangJenis.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbBarangJenis.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbBarangJenisMousePressed(evt);
            }
        });
        tbBarangJenis.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbBarangJenisKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbBarangJenisKeyReleased(evt);
            }
        });
        spBarangJenis.setViewportView(tbBarangJenis);

        pnTengah.add(spBarangJenis, java.awt.BorderLayout.CENTER);

        pnAtas.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas.setPreferredSize(new java.awt.Dimension(410, 220));
        pnAtas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        pnAtas.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 160, 70, 20));

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        pnAtas.add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 130, 70, -1));
        pnAtas.add(txtNamaGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 40, 230, -1));

        jLabel2.setText("Nama Jenis");
        pnAtas.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

        txtKdJenis.setEditable(false);
        pnAtas.add(txtKdJenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 80, -1));

        jLabel3.setText("Golongan");
        pnAtas.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jLabel4.setText("Keterangan");
        pnAtas.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, -1, -1));

        txtKet.setColumns(20);
        txtKet.setRows(5);
        jScrollPane1.setViewportView(txtKet);

        pnAtas.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 100, 220, 57));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        pnAtas.add(btnSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 190, -1, -1));
        pnAtas.add(txtNamaSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 230, -1));

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        pnAtas.add(btnNew, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 100, 70, -1));

        jLabel6.setText("Kode Jenis");
        pnAtas.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));
        pnAtas.add(txtNamaJenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, 320, -1));

        cbGol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbGol.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbGolItemStateChanged(evt);
            }
        });
        pnAtas.add(cbGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 40, 80, -1));

        cbSearchJenis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Jenis", "Nama Jenis" }));
        pnAtas.add(cbSearchJenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 100, -1));

        btnShowAll.setText("Show All");
        btnShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllActionPerformed(evt);
            }
        });
        pnAtas.add(btnShowAll, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 190, 80, -1));

        pnTengah.add(pnAtas, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(pnTengah, java.awt.BorderLayout.CENTER);

        setSize(new java.awt.Dimension(428, 361));
    }// </editor-fold>//GEN-END:initComponents

    private void tbBarangJenisMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbBarangJenisMousePressed
        getDataFromTable();
}//GEN-LAST:event_tbBarangJenisMousePressed

    private void tbBarangJenisKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBarangJenisKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tbBarangJenisKeyPressed

    private void tbBarangJenisKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBarangJenisKeyReleased
        // TODO add your handling code here:
        getDataFromTable();
}//GEN-LAST:event_tbBarangJenisKeyReleased

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(btnDelete.getText().equals("Cancel")){
            kosongkan();
            tampilData();
        }else if(btnDelete.getText().equals("Delete")){
            deleteData();
        }

        //*
        //*/
}//GEN-LAST:event_btnDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        //BARU KOPAS
        if(btnEdit.getText().equals("Save")){
            if(txtNamaJenis.getText().equals("") )
            {
                JOptionPane.showMessageDialog(this, "Nama jenis harus diisi!",
                        "Informasi",JOptionPane.INFORMATION_MESSAGE);
            }else{
            insertData();
                
            }
        }else if (btnEdit.getText().equals("Edit")){
            editData();
        }

        //*
        //*/
}//GEN-LAST:event_btnEditActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        //BARU KOPAS

        tampilTabelSearch();
        txtKdJenis.setText("");
        txtKdJenis.requestFocus();
        txtNamaGol.setText("");
        txtNamaJenis.setText("");
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        //*
        //*/
}//GEN-LAST:event_btnSearchActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:               
        txtNamaGol.setText("");
        txtNamaJenis.setText("");
        txtKet.setText("");
        txtNamaSearch.setText("");
        btnEdit.setText("Save");
        btnDelete.setText("Cancel");
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        setKodeJenis();
        setDataGolongan();
}//GEN-LAST:event_btnNewActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Tutup Master Jenis Barang?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isfBrgJenis = false;
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void cbGolItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbGolItemStateChanged
        // TODO add your handling code here:
        if(isGolLoaded==true){
            setDataGolongan();
            setKodeJenis();
        }
    }//GEN-LAST:event_cbGolItemStateChanged

    private void btnShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllActionPerformed
        // TODO add your handling code here:
        tampilData();
        txtNamaSearch.setText(""); 
}//GEN-LAST:event_btnShowAllActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnShowAll;
    private javax.swing.JComboBox cbGol;
    private javax.swing.JComboBox cbSearchJenis;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnAtas;
    private javax.swing.JPanel pnTengah;
    private javax.swing.JScrollPane spBarangJenis;
    private javax.swing.JTable tbBarangJenis;
    private javax.swing.JTextField txtKdJenis;
    private javax.swing.JTextArea txtKet;
    private javax.swing.JTextField txtNamaGol;
    private javax.swing.JTextField txtNamaJenis;
    private javax.swing.JTextField txtNamaSearch;
    // End of variables declaration//GEN-END:variables

    private void kosongkan(){
        txtNamaJenis.setText("");
        txtKet.setText("");
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        btnEdit.setText("Edit");
        btnDelete.setText("Delete");

    }
    private void tampilData(){
        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbBarangJenis.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_jenis");
            while (rslt.next()) {
                int no = rslt.getRow();
                String sKdJenis = rslt.getString("kdJenis");
                String sNamaJenis = rslt.getString("nmJenis");
                //String sKet = rslt.getString("ket");
                //String sTest = rslt.getString("test");

                String[] dataField = {Integer.toString(no), sKdJenis, sNamaJenis};//, sKet};
                DftTabMode.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void tampilTabelSearch(){
        /*
        String[] columnNames =
        {"No","Kode","Kota"};

        table = new  JTable(
                getDataSearch(), columnNames);
        spKota.setViewportView(table);
        */
        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbBarangJenis.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg_jenis WHERE UPPER(kdJenis) like UPPER('%" +
                    txtNamaSearch.getText() + "%')");
            //int iNo = 0;
            while (rslt.next()) {
                //iNo = iNo + 1;
                //iNo.
                int x = rslt.getRow();
                //
                String sKdJenis = rslt.getString("kdJenis");
                String sNamaJenis = rslt.getString("nmJenis");
                //String sNamaKota = rslt.getString("ket");

                String[] dataField = {Integer.toString(x), sKdJenis, sNamaJenis};
                DftTabMode.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }
    private void deleteData(){
            try {
                int deleting;
                deleting = JOptionPane.showConfirmDialog(this,
                        "Apakah Anda yakin akan menghapus "

                        //+ cbKodeKota.getSelectedItem().toString() +" "
                        +txtKdJenis.getText() +"?",
                        "Konfirmasi",
                        JOptionPane.YES_NO_OPTION);
                if (deleting == 0){

                    PreparedStatement pStatement = null;
                    String sql = "DELETE FROM tbrg_jenis " +
                            "WHERE kdJenis=? ";
                    pStatement = conn.prepareStatement(sql);

                    pStatement.setString(1, txtKdJenis.getText());

                    int intTambah = pStatement.executeUpdate();
                    if (intTambah >0){
                        JOptionPane.showMessageDialog(this,
                                "Delete sukses", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);

                    }else {
                        JOptionPane.showMessageDialog(this,
                                "Delete gagal", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);
                    }

                    pStatement.close();

                    kosongkan();
                    tampilData();
                }
            } catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
    }
    private void insertData(){

        java.sql.PreparedStatement pStatement = null;
        String sql = "INSERT INTO tbrg_jenis " +
                "(kdJenis, kdGol, nmJenis, ket, tgl_insert, tgl_update, user_id) " +
                "VALUES (?,?,?,?, now(),now(),'admin')";
        try {
            pStatement = conn.prepareStatement(sql);
            pStatement.setString(1, txtKdJenis.getText());            
            String kdGol = (String)cbGol.getSelectedItem();
            pStatement.setString(2, kdGol);
            pStatement.setString(3, txtNamaJenis.getText());
            pStatement.setString(4, txtKet.getText());

            int intTambah = pStatement.executeUpdate();
            if (intTambah>0){
                JOptionPane.showMessageDialog(this,
                        "Penambahan sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this,
                        "Penambahan gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            pStatement.close();
            //conn.close();

            kosongkan();
            tampilData();

        }
        catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }

     private void editData(){
         //nomor ieu mah
        //String sKdGol = tbBarangGol.getValueAt(
               // tbBarangGol.getSelectedRow(), 0).toString();

        try {
            PreparedStatement pStatement = null;
            String sql = "UPDATE tbrg_jenis " +
                    "SET nmJenis=?, ket=? " +
                    "WHERE kdJenis=? ";
            pStatement = conn.prepareStatement(sql);

            //pStatement.setString(1, cbKodeKota.getSelectedItem().toString());
            //pStatement.setString(1, txtKdGol.getText());
            pStatement.setString(1, txtNamaJenis.getText());
            pStatement.setString(2, txtKet.getText());
            pStatement.setString(3, txtKdJenis.getText());

            int intTambah = pStatement.executeUpdate();
            if (intTambah >0){
                JOptionPane.showMessageDialog(this,
                        "Edit sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);

            }else {
                JOptionPane.showMessageDialog(this,
                        "Edit gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }

            //tbBarangGol.get
            pStatement.close();
            kosongkan();
            tampilData();


        } catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }
     
    private void getDataFromTable(){
                // TODO add your handling code here:
        Object oKdJenis, oNamaJenis, oKdGol;//, oKet;//, oTest;

        oKdJenis = tbBarangJenis.getValueAt(tbBarangJenis.getSelectedRow(), 1);
        oNamaJenis = tbBarangJenis.getValueAt(tbBarangJenis.getSelectedRow(), 2);
        //oKet = tbBarangGol.getValueAt(tbBarangGol.getSelectedRow(), 2);
        //oTest = tbKota.getValueAt(tbKota.getSelectedRow(), 3);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_jenis WHERE kdJenis=" +
                    "'" +oKdJenis+ "'");
            while (rslt.next()) {
                oKdGol = rslt.getString("kdGol");
                cbGol.setSelectedItem(oKdGol);
                txtKet.setText(rslt.getString("ket"));
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        /*
        if(oTest == null){
            oTest = " ";
        }
         *
         */

        txtKdJenis.setText(oKdJenis.toString());
        
        txtNamaJenis.setText(oNamaJenis.toString());
        //txtKet.setText(oKet.toString());
        //txtTest.setText(oTest.toString());

        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        setDataGolongan();
    }

    private void loadDataGolongan(){
        cbGol.removeAllItems();

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol");
            while (rslt.next()) {
                Object data = (Object)rslt.getString("kdGol");
                cbGol.addItem(data);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        isGolLoaded = true;
    }
    private void setDataGolongan(){
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol " +
                    "WHERE kdGol='" + cbGol.getSelectedItem().toString() + "'");
            while (rslt.next()) {
                txtNamaGol.setText(rslt.getString("nmGol"));                
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void setKodeJenis(){
        
        try {
            String kdGol = cbGol.getSelectedItem().toString();
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_jenis " +
                    "WHERE kdGol='" + kdGol + "'");
            int x = 1;
            while (rslt.next()) {
                x = x + 1;//rslt.getRow();
                //System.out.println(x);
            }
            //System.out.println(kdGol);
            
            java.text.DecimalFormat nft = new java.text.DecimalFormat("#000.###");
            nft.setDecimalSeparatorAlwaysShown(false);
            txtKdJenis.setText(kdGol + "." + nft.format(x));
            //System.out.println(txtKdJenis.getText());

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
         
    }

    
    
}